﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class ContragentFLEditForm : Form
    {
        private string mode;

        private Form parentForm;

        public Contragent contragent;

        private bool act = false;

        public ContragentFLEditForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Конструктор на добавление
        /// </summary>
        /// <param name="parentForm"></param>
        /// <param name="mode"></param>
        public ContragentFLEditForm(Form parentForm, string mode)
        {
            InitializeComponent();

            this.mode = mode;

            this.Text = string.Format("Контрагент (Физ.лицо) [{0}]", mode);

            this.parentForm = parentForm;

            if (this.mode == "Добавление")
            {
                contragent = new Contragent();
                act = true;
            }
        }

        /// <summary>
        /// Конструктор на изменение
        /// </summary>
        /// <param name="parentForm"></param>
        /// <param name="mode"></param>
        /// <param name="id"></param>
        public ContragentFLEditForm(Form parentForm, string mode, int id, string type)
        {
            InitializeComponent();

            this.mode = mode;

            this.Text = string.Format("Контрагент (Физ.лицо) [{0}]", mode);

            this.parentForm = parentForm;

            if (mode == "Изменение")
            {
                contragent = new Contragent();

                Tools.RunWithProgressIndicator(this, delegate
                {
                    contragent.typeContragent = type;

                    contragent.SelectById(id, this.contragent.typeContragent);

                    tbxFamily.Text = contragent.Family;
                    tbxName.Text = contragent.Name;
                    tbxFName.Text = contragent.FName;
                    mtbxPassportNumber.Text = contragent.PassportNumber;
                    mtbxPassportSeries.Text = contragent.PassportSeries;
                    tbxPassportVidan.Text = contragent.PassportVidan;

                    tbxTelHome.Text = contragent.telHome;
                    tbxTelWork.Text = contragent.telWork;
                    tbxTelMobile.Text = contragent.telMobile;
                    tbxNote.Text = contragent.note;
                    tbxEmail.Text = contragent.email;

                    tbxAddressText.Text = contragent.address.RebuildAddressText();

                    rbtnMale.Checked = contragent.Male;
                    rbtnFemale.Checked = !contragent.Male;

                });
            }

            this.act = true;
        }

        private void rebuildContragentData(object sender, EventArgs e)
        {
            if (act)
            {
                this.contragent.Family = tbxFamily.Text.Trim();
                this.contragent.Name = tbxName.Text.Trim();
                this.contragent.FName = tbxFName.Text.Trim();
                this.contragent.Birth = dateTimePickerBirth.Value;
                this.contragent.Male = rbtnMale.Checked;
                this.contragent.PassportNumber = mtbxPassportNumber.Text;
                this.contragent.PassportSeries = mtbxPassportSeries.Text;
                this.contragent.PassportVidan = tbxPassportVidan.Text;

                this.contragent.telHome = tbxTelHome.Text.Trim();
                this.contragent.telMobile = tbxTelMobile.Text.Trim();
                this.contragent.telWork = tbxTelWork.Text.Trim();
                this.contragent.note = tbxNote.Text.Trim();
                this.contragent.email = tbxEmail.Text.Trim();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool bSuccessfully = false;

            try
            {
                if (!(contragent.address.id_address != 0 &&
                      contragent.Family.Length != 0 &&
                      contragent.Name.Length != 0))
                {
                    MessageBox.Show("Не все обязательные для заполнения поля заполнены", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                contragent.typeContragent = "ФЛ";

                if (this.mode == "Изменение")
                {
                    Tools.RunWithProgressIndicator(this, delegate { contragent.Update(); });

                    MessageBox.Show("Контрагент изменен успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    if (mode == "Добавление")
                    {
                        Tools.RunWithProgressIndicator(this, delegate { contragent.Insert(); });

                        MessageBox.Show("Контрагент добавлен успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                //if (parentForm is RezidentForm)
                //{
                //    (parentForm as RezidentForm).UpdateForm();
                //}

                bSuccessfully = true;
            }
            catch
            {
                ;
            }

            if (bSuccessfully)
            {
                this.Close();
            }
        }

        private void btnAddressChange_Click(object sender, EventArgs e)
        {
            AddressEditForm addressEditForm;

            if (contragent.address.id_address != 0)
            {
                addressEditForm = new AddressEditForm(this, "Изменение", contragent.address.id_address);
            }
            else
            {
                addressEditForm = new AddressEditForm(this, "Добавление");
            }

            addressEditForm.ShowDialog();

            tbxAddressText.Text = addressEditForm.address.RebuildAddressText();
            contragent.address = addressEditForm.address;
        }
    }
}
